# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html


class CsdnBlogPipeline(object):
    def __init__(self, cookies):
        self.cookies = cookies

    def process_item(self, item, spider):
        return item

    @classmethod
    def from_crawler(cls, crawler):
        cookies_str = crawler.settings.get('USER_COOKIES')
        # 格式化cookie
        cookie_list = cookies_str.split('; ')
        cookies = {}
        for cookie in cookie_list:
            cookies[cookie.split('=')[0]] = cookie.split('=')[1]
        return cls(cookies=cookies)

    def open_spider(self, spider):
        spider.cookies = self.cookies

    def close_spider(self, spider):
        pass
